#include "lb_modifybook.h"
#include "ui_lb_modifybook.h"


#include <QStandardItemModel>
#include <QTableView>
#include <QDebug>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QIODevice>
#include <QMessageBox>
#include <QPalette>


//pass
LB_modifybook::LB_modifybook(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LB_modifybook)
{
    ui->setupUi(this);

    //QPixmap pixmap = QPixmap("D://Qt//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//flower.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);


    this->model = new QStandardItemModel;//new出来一个表格模型

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,150);
    this->ui->tableView->setColumnWidth(1,200);

}

//pass
LB_modifybook::~LB_modifybook()
{
    delete ui;
}

//pass
void LB_modifybook::on_btn_search_clicked()//用户点击搜索后调用此函数
{


    this->model->clear();//清空上一次搜索显示

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,150);
    this->ui->tableView->setColumnWidth(1,200);

    //搜索的核心代码
    QString needToModified = this->ui->le_modify_search->text();//获取用户输入

    int row = 0;

    for(int i = 0;i < 100 ;i++){
        if(dataArray[i].bookTitle.contains(needToModified, Qt::CaseInsensitive)&&dataArray[i].existOrNot){

            //display(i)
            this->model->setItem(row,0,new QStandardItem(dataArray[i].isbn));
            this->model->setItem(row,1,new QStandardItem(dataArray[i].bookTitle));

            row++;
        }
    }
}

//pass
void LB_modifybook::modifyBookInfo()//此函数涉及到底层数据的修改，因此在函数结束前要覆盖底层文件
{
    QString input_isbn = this->ui->le_modify_isbn->text();

    bool cb_isbn = ui->checkBox_isbn->isChecked();
    bool cb_title = ui->checkBox_title->isChecked();
    bool cb_author = ui->checkBox_author->isChecked();
    bool cb_pub = ui->checkBox_publish->isChecked();
    bool cb_time = ui->checkBox_time->isChecked();
    bool cb_qty = ui->checkBox_qty->isChecked();
    bool cb_whole = ui->checkBox_wholesale->isChecked();
    bool cb_retail = ui->checkBox_retail->isChecked();

    QString content;
    for(int i = 0;i < 100 ;i++){
        if(input_isbn == dataArray[i].isbn){//如果找到这本书

            //先判断哪些复选框为true，然后将后面的line edit中的输入复制到对象数组中
            if(cb_isbn) {dataArray[i].isbn = this->ui->le_isbn->text();content = content + "新的ISBN:" + dataArray[i].isbn + '\n';}
            if(cb_title) {dataArray[i].bookTitle = this->ui->le_title->text();content = content + "新的书名：" + dataArray[i].bookTitle + '\n';}
            if(cb_author) {dataArray[i].author = this->ui->le_author->text();content = content + "新的作者：" + dataArray[i].author + '\n';}
            if(cb_pub) {dataArray[i].publisher = this->ui->le_publish->text();content = content + "新的出版社：" + dataArray[i].publisher + '\n';}
            if(cb_time) {dataArray[i].dataAdded = this->ui->le_time->text();content = content + "新的进书日期：" + dataArray[i].dataAdded + '\n';}
            if(cb_qty) {dataArray[i].qtyOnHand = this->ui->le_qty->text();content = content + "新的库存量：" + dataArray[i].qtyOnHand + '\n';}
            if(cb_whole) {dataArray[i].wholesale = this->ui->le_wholesale->text();content = content + "新的批发价：" + dataArray[i].wholesale + '\n';}
            if(cb_retail) {dataArray[i].retail = this->ui->le_retail->text();content = content + "新的零售价：" + dataArray[i].retail + '\n';}
            break;
        }
    }

    QMessageBox::question(this,"请确认信息",content,"确认","取消");

}

//pass
void LB_modifybook::on_btn_sure_clicked()//用户修改完信息后点击此按钮，进行底层数据修改
{
    modifyBookInfo();
}

void LB_modifybook::on_pushButton_clicked()
{
    this->close();
}
